<?php

namespace Laoha\Core;

use Encore\Admin\Form;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Grid;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Controllers\HasResourceActions;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Schema;

class CateController extends Controller
{
	use HasResourceActions;
	protected $table_id=0;
    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
		$id = request('table_id');
		if($id){
			//$table = LaohaTable::where('id',$id)->with('cates')->get();
			//dd($table);
			$table = LaohaTable::where([['id','=',$id],['allow_cate','=',1]])->orWhere([['name','=',$id],['allow_cate','=',1]])->first();
			
			if(! $table){
				return $content->title('错误')->body('<div class="alert alert-warning">未指定数据表或数据表不允许分类</div>');
			}
			
			$this->table_id = $table->id;
			return $content
				->breadcrumb( 
					['text' => '可分类表', 'url' => '/laoha/cate'],
					['text' => $table->name.'表分类']
				)
				->title($table->name.'表分类管理')
				->description(trans('admin.list'))
				->row(function (Row $row)  use ($table){
					$row->column(6, $this->treeView()->render());
					
					$row->column(6, function (Column $column) use ($table){
						$form = new \Encore\Admin\Widgets\Form();
						$form->action(admin_base_path('laoha/cate').'?table_id='.$this->table_id);

						$menuModel = LaohaCate::class;

						$form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());

						$form->text('name', trans('admin.name'))->rules('required');
						$form->text('title', trans('admin.title'))->rules('required');
						$form->textarea('sub', '分组')->help('一行一个分组项目');

						$form->hidden('table_id', 'table')->default($this->table_id);

						$column->append((new Box(trans('admin.new'), $form))->style('success'));
					});
				});
		}else{
			return $content
				->title('可分类表')
				->description('选择指定表进行分类管理')
				->body($this->grid());		
		}
    }

    protected function grid()
    {  
        $tables = new LaohaTable();
		//$tables = $tables->allowCate();
		$grid = new Grid($tables);
		
		$grid->model()->allowCate();
		$grid->disableActions();

		$grid->disablePagination();

		$grid->disableCreateButton();

		$grid->disableFilter();

		$grid->disableRowSelector();

		$grid->disableColumnSelector();

		$grid->disableTools();

		$grid->disableExport();


		
        $grid->column('name', '表名');
		$grid->column('id', '管理')->display(function ($id) {
			return "<a href='".route('admin.laoha.cate.index', ['table_id' => $id])."' style='color:blue'>管理分类</span>";
		});

        return $grid;
    }

    /**
     * Redirect to edit page.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function show($id)
    {
        $cate = LaohaCate::find($id);
		return redirect()->route('admin.laoha.cate.edit', ['table'=>$cate['table_id'],'id' => $id]);
    }

    /**
     * @return \Encore\Admin\Tree
     */
    protected function treeView()
    {
        $menuModel = new LaohaCate($this->table_id);
        return $menuModel::tree(function (Tree $tree){
            $tree->disableCreate();

            $tree->branch(function ($branch) {
                $payload = "&nbsp;<strong>{$branch['title']}</strong> -- {$branch['name']}";

                return $payload;
            });
        });
    }

    /**
     * Edit interface.
     *
     * @param string  $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
		return $content
            ->title('分类')
            ->description(trans('admin.edit'))
            ->row($this->form()->edit($id));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {

		$menuModel = LaohaCate::class;

        $form = new Form(new $menuModel());
        $form->display('id', 'ID');

        $form->select('parent_id', trans('admin.parent_id'))->options($menuModel::selectOptions());
		$form->text('name', trans('admin.name'))
			->rules('required|regex:/^[A-Za-z][A-Za-z0-9_]+$/',['regex' => '名称必须是英文开头且只包含字母、数字、下划线',]);
        $form->text('title', trans('admin.title'))->rules('required');
		$form->textarea('sub', '分组')->help('一行一个分组项目');
		$form->hidden('table_id', 'table');

        return $form;
    }

    /**
     * Help message for icon field.
     *
     * @return string
     */
    protected function iconHelp()
    {
        return 'For more icons please see <a href="http://fontawesome.io/icons/" target="_blank">http://fontawesome.io/icons/</a>';
    }


}
